import java.awt.*;
import java.awt.event.*;
import java.sql.*;

@SuppressWarnings("serial")
public class Etc extends Frame implements ActionListener, ItemListener{
	public static final int  NONE   = 0;
	public static final int  ADD    = 1;
	public static final int  DELETE = 2;
	public static final int  SEARCH = 3;
	public static final int  TOTAL  = 4;
	
	public static TextArea  display;
	public static TextField  Ename;
	public static Choice Year, DM, DD, DT;
	public static Button     add, delete, search, total, etc, cancel;
	public static Connection con;
	public static Statement  stat;
	public static int        cmd, ytemp, smtemp;
	public static ResultSet rs = null;
	public static TextField        tf_temp;
	public static Choice        ch_temp;
	
	public Etc() {
		super("Etc");
		init();
		setLayout(new BorderLayout());
		display = new TextArea();
		display.setEditable(false);

		Panel left = new Panel();
		left.setLayout(new GridLayout(5,1));
		
		Panel year = new Panel();
		year.add(new Label("Year"));
		year.add(Year = new Choice());
		Year.add("2009");
		Year.add("2010");
		Year.add("2011");
		Year.add("2012");
		
		String year_string = Year.getSelectedItem();
		int year_int = Integer.parseInt(year_string);
		Year.addItemListener(this);
		ytemp = year_int;
		
		year.add(new Label("Month"));
		year.add(DM = new Choice());
		for(int i = 1 ; i < 13 ; i++) {
			DM.add(""+i+"");
		}
		String dm_string = DM.getSelectedItem();
		int dm_int = Integer.parseInt(dm_string);
		DM.addItemListener(this);
		smtemp = dm_int;
		
		Panel HW = new Panel();
		HW.add(new Label("Event name"));
		HW.add(Ename = new TextField(20));
		
		
		Panel Dmonth = new Panel();
		
		Dmonth.add(new Label("Day"));
		Dmonth.add(DD = new Choice());
		for(int i = 1 ; i < 32 ; i++) {
			DD.add(""+i+"");
		}
		
		Panel DTime = new Panel();
		DTime.add(new Label("Time"));
		DTime.add(DT = new Choice());
		for(int i = 0 ; i < 24 ; i++) {
			DT.add(""+i+"");
		}
		
		
		left.add(year);
		left.add(HW);
		left.add(Dmonth);
		left.add(DTime);
		
		
		Panel bottom = new Panel();
		bottom.add(add = new Button("Add"));
		add.addActionListener(this);
		bottom.add(delete = new Button("Delete"));
		delete.addActionListener(this);
		bottom.add(total = new Button("All"));
		total.addActionListener(this);
		bottom.add(cancel = new Button("Cancle"));
		cancel.addActionListener(this);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				destroy();
				setVisible(false);
				dispose();
				//System.exit(0);
			}
		});
		add("Center", display);
		add("West", left);
		add("South", bottom);
		cmd = NONE;
		initialize();
	}

	private void init() {
		try{
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		}catch(ClassNotFoundException ce){
			System.out.println(ce);
		}
		try{
			con =DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Seoyoung", "sa", "pw");
			System.out.println("You access DBMS because of Diary");
			stat = con.createStatement();
		}catch(SQLException se){
			System.out.println(se);
		}finally{
			//try{
			//}catch(SQLException se){}
		}
		
	}


	public void initialize() {
		Ename.setEditable(false);
		Year.setEnabled(false);
		DM.setEnabled(false);
		DD.setEnabled(false);
		DT.setEnabled(false);
		Ename.setText("1st, Fit for purpose and press the button.");
		
	}

	private void destroy() {
		try {
			if(stat != null) {
				stat.close();
			}
			if(con != null) {
				con.close();
			}
			if(rs != null){
				rs.close();
			}
		} catch(Exception ex) { }
	}

	public static void setEditable(int n) {
		switch(n) {
		case ADD:
			Ename.setEditable(true);
			Year.setEnabled(true);
			DM.setEnabled(true);
			DD.setEnabled(true);
			DT.setEnabled(true);
			break;
			
		case DELETE:
			Ename.setEditable(true);
			Year.setEnabled(true);
			DM.setEnabled(true);
			DD.setEnabled(true);
			break;
			
		case NONE:
			Ename.setEditable(false);
			Year.setEnabled(false);
			DM.setEnabled(false);
			DD.setEnabled(false);
			DT.setEnabled(false);
			Ename.setText("1st, Fit for purpose and press the button.");
			break;
			
		case TOTAL:
			break;
		}	
	}	
	

	public static void setEnable(int n) {
		add.setEnabled(false);
		delete.setEnabled(false);
		total.setEnabled(false);
		switch(n) {
			case ADD:
				add.setEnabled(true);
				setEditable(ADD);
				cmd = ADD;
				break;
			case DELETE:
				delete.setEnabled(true);
				setEditable(DELETE);
				cmd = DELETE;
				break;
			case TOTAL:
				total.setEnabled(true);
				cmd = TOTAL;
				break;
			case NONE:
				add.setEnabled(true);
				delete.setEnabled(true);
				total.setEnabled(true);
		}
		
	}

	public void clear() {
		
		
	}

	public void actionPerformed(ActionEvent e) {
		Component c = (Component) e.getSource();
		try {
			if(c == add) {
				if(cmd != ADD){
					setEnable(ADD);
					System.out.println("You are going to adding Someting...");
				}else {
					
					String E_string = Ename.getText().trim();
					String year_string = Year.getSelectedItem();
					String DM_s = DM.getSelectedItem();
					String DD_s = DD.getSelectedItem();
					String DT_s = DT.getSelectedItem();
				
					int year_int = Integer.parseInt(year_string);
					int DM_int = Integer.parseInt(DM_s);
					int DD_int = Integer.parseInt(DD_s);
					int DT_int = Integer.parseInt(DT_s);
					
					if(E_string == null || E_string.length() == 0)
							return ;
					String sql = "insert into Event values('" + E_string + "', " + DM_int +","+ DD_int 
					+","+ year_int +", " +DT_int +")";
					stat.executeUpdate(sql);
					System.out.println("Successful insertion!");
					setEnable(NONE);
					clear();
					cmd = NONE;
					initialize();
				}
			}else if(c == total){
				rs = stat.executeQuery("select * from Event order by Year, Month, Day, Time;");
				display.setText("================================================================" +
						"==============================================" +"\n");
				display.append("\tEvent \t\t\t\t\t Date \t\t Time \t\n");
				display.append("===============================================================================" +
						"===============================" +"\n");
				while(rs.next()) {
					String E_name = rs.getString(1);
					int year = rs.getInt(2);
					int mon = rs.getInt(3);
					int day =  rs.getInt(4);
					int time = rs.getInt(5);
					
					display.append("\t"+ E_name + " \t\t\t" + year +"." + mon + "." + day + "\t\t  " + time + "\n");
					display.append("---------------------------------------------------------------------" +
							"-----------------------------------------\n");
					cmd = NONE;
				}
			}else if(c == delete){
				if(cmd != DELETE){
					setEnable(DELETE);
					System.out.println("You are going to deleting Someting...");
				}else {
					String E_string = Ename.getText().trim();
					String year_string = Year.getSelectedItem();
					String DM_s = DM.getSelectedItem();
					String DD_s = DD.getSelectedItem();
					//String DT_s = DT.getSelectedItem();
				
					int year_int = Integer.parseInt(year_string);
					int DM_int = Integer.parseInt(DM_s);
					int DD_int = Integer.parseInt(DD_s);
					//int DT_int = Integer.parseInt(DT_s);
					if(E_string == null || E_string.length() == 0)
						return;
					String sql = "delete from Event where EName='"+E_string + "' and Month = " + DM_int+ " and "
					+ "Day = " + DD_int + " and Year=" + year_int;
					System.out.println(sql);
					stat.executeUpdate(sql);
					System.out.println("Successful Deletaion!");
					setEnable(NONE);
					clear();
					cmd = NONE;
					initialize();
					
				}
				
			}else if(c == cancel) {
				setEnable(NONE);
				initialize();
				cmd = NONE;
			}
		}catch(Exception ex) { }
		return ;
	}
	
	public void itemStateChanged(ItemEvent e){
		Component c = (Component) e.getSource();
		if(c == DM){
			DD.removeAll();
			String year_string = Year.getSelectedItem();
			ytemp = Integer.parseInt(year_string);
			String sm_string = DM.getSelectedItem();
			smtemp = Integer.parseInt(sm_string);
			if(smtemp == 1 || smtemp == 3 || smtemp == 5 || smtemp == 7 || smtemp == 8 || smtemp == 10 || smtemp == 12){
				for(int i = 1 ; i < 32 ; i++) {
					DD.add(""+i+"");
				}
			}else if(smtemp == 2){
				if(ytemp == 2012)
					for(int i = 1 ; i < 30 ; i++) {
						DD.add(""+i+"");
					}
				
				else
					for(int i = 1 ; i < 29 ; i++) {
						DD.add(""+i+"");
					}
				
			}else{
				for(int i = 1 ; i < 31 ; i++) {
					DD.add(""+i+"");
				}
			}
		}
		return;
	}
	
}
